package com.ra.mi.test

import android.content.ComponentName
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.ra.mi.test.ui.theme.TestTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            TestTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Row (modifier = Modifier.padding(horizontal = 8.dp, vertical = 58.dp)){
                        Column{
                            Greeting("default", onButtonClick = {
                                setAppIcon("default")
                            })

                        }
                        Column{

                            Greeting("green", onButtonClick = {
                                setAppIcon("green")
                            })

                        }
                        Column{


                            Greeting("yellow", onButtonClick = {
                                setAppIcon("yellow")
                            })

                        }
                        Column{
                            Greeting("red", onButtonClick = {
                                setAppIcon("red")
                            })
                        }
                    }
                }
            }
        }
    }
    private fun setAppIcon(iconId: String) {
        val icon = appIcons.find { it.id == iconId } ?: return
        val context = baseContext
        val packageManager = context.packageManager

        appIcons.filterNot { it.id == iconId }.forEach {
            packageManager.setComponentEnabledSetting(
                ComponentName(context, it.component),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP
            )
        }

        packageManager.setComponentEnabledSetting(
            ComponentName(context, icon.component),
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP
        )
    }
}

@Composable
fun Greeting(name: String,onButtonClick: () -> Unit, modifier: Modifier = Modifier) {
    Button(
        onClick = onButtonClick,
        contentPadding = PaddingValues(all = 20.dp),
        elevation = ButtonDefaults.buttonElevation(defaultElevation = 5.dp),
        modifier = modifier
    ){
        Text("$name")
    }

}



@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    TestTheme {
        Row {
            Column{
                Greeting("default", onButtonClick = {

                })
            }
        }
    }
}


